package com.graduate.front.compont;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.graduate.common.entity.Users;
import com.graduate.common.mapper.UsersMapper;
import com.graduate.front.pojo.MyUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

/**
 * @author bwzfy
 * @create 2021/5/16
 **/
@SuppressWarnings("ALL")
@Component
public class MyUserDetailsService implements UserDetailsService {

    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 通过用户名来查询用户信息
        QueryWrapper<Users> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        Users users = usersMapper.selectOne(wrapper);
        MyUser myUser = new MyUser();
        BeanUtils.copyProperties(users, myUser);
        myUser.setPassword(passwordEncoder.encode(myUser.getPassword()));
        return myUser;
    }
}
